package cn.lili.modules.grab.dingdong.service.impl;

import cn.lili.cache.util.RedisUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.exchange.AmqpExchangeProperties;
import cn.lili.modules.grab.aop.request.RequestLogAnntion;
import cn.lili.modules.grab.dingdong.DingdongBody;
import cn.lili.modules.grab.dingdong.base.BaseResult;
import cn.lili.modules.grab.dingdong.config.DingdongConfig;
import cn.lili.modules.grab.dingdong.facade.DingdongFacade;
import cn.lili.modules.grab.dingdong.reps.adjunct.RegionAllRespose;
import cn.lili.modules.grab.dingdong.reps.order.CreateOrderRespose;
import cn.lili.modules.grab.dingdong.reps.order.FreightCalcRespose;
import cn.lili.modules.grab.dingdong.reps.order.GetOrderInfoRespose;
import cn.lili.modules.grab.dingdong.res.adjunct.RegionAllRequest;
import cn.lili.modules.grab.dingdong.res.order.CreateOrderRequest;
import cn.lili.modules.grab.dingdong.res.order.FreightCalcRequest;
import cn.lili.modules.grab.dingdong.res.order.GetOrderInfoRequest;
import cn.lili.modules.grab.dingdong.service.DingdongAbsService;
import cn.lili.modules.grab.dingdong.service.DingdongAdjunctService;
import cn.lili.modules.grab.dingdong.service.DingdongOrderService;
import cn.lili.modules.system.constants.RequestLogTypeConstant;
import cn.lili.util.AmqpSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class DingdongOrderServiceImpl extends DingdongAbsService implements DingdongOrderService {

    @Resource
    private DingdongFacade dingdongFacade;
    @Resource
    private DingdongConfig dingdongConfig;
    @Resource
    private DingdongAdjunctService dingdongAdjunctService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AmqpExchangeProperties amqpExchangeProperties;
    @Resource
    private AmqpSender amqpSender;

    /**
     * 运费计算
     *
     * @param freightCalcRequest 请求内容
     */
    @Override
    public FreightCalcRespose freightCalc(FreightCalcRequest freightCalcRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(freightCalcRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getFreightCalcUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            FreightCalcRespose result = JSONObject.parseObject(String.valueOf(baseResult.getContent()), FreightCalcRespose.class);
            return result;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    /**
     * 创建订单
     *
     * @param createOrderRequest 请求内容
     */
    @Override
    //@RequestLogAnntion(type = RequestLogTypeConstant.QI_DING_DONG_CREATE_ORDER,typeName = RequestLogTypeConstant.QI_DING_DONG_CREATE_ORDER_NAME)
    public CreateOrderRespose createOrder(CreateOrderRequest createOrderRequest) {

        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(createOrderRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getCreateOrderUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            CreateOrderRespose result = JSONArray.parseObject(String.valueOf(baseResult.getContent()), CreateOrderRespose.class);
            result.setBaseResult(baseResult);
            return result;
        }

        throw new ServiceException(baseResult.getMsg());
    }


    /**
     * 订单详情
     *
     * @param getOrderInfoRequest 请求内容
     */
    @Override
    public List<GetOrderInfoRespose> getOrderInfo(GetOrderInfoRequest getOrderInfoRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(getOrderInfoRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGetOrderInfoUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            JSONObject json = JSONObject.parseObject(String.valueOf(baseResult.getContent()));
            List<GetOrderInfoRespose> getOrderInfoResposeList = JSONArray.parseArray(json.getString("data"), GetOrderInfoRespose.class);
            return getOrderInfoResposeList;
        }
        throw new ServiceException(baseResult.getMsg());
    }
}
